package com.fancy.projecty.controller;

import com.fancy.projecty.mapper.AskForLeaveMapper;
import com.fancy.projecty.pojo.AskForLeave;
import com.fancy.projecty.pojo.bo.FlowDisposeBO;
import com.fancy.projecty.pojo.bo.NodeExamineBo;
import com.fancy.projecty.pojo.bo.ProcessDefinitionBO;
import com.fancy.projecty.pojo.bo.ActRuTaskBO;
import com.fancy.projecty.util.ExceptionUtil;
import com.fancy.projecty.util.SuccessVo;
import com.fancy.projecty.util.UuidUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.activiti.engine.IdentityService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 请假Controller
 */
@RequestMapping("/askForLeave")
@RestController
@CrossOrigin
@Api(tags = "请假流程")
public class AskForLeaveController {
	@Autowired
	private AskForLeaveMapper askForLeaveMapper;
	@Autowired
	private IdentityService identityService;
	@Autowired
	private RuntimeService runtimeService;
	@Autowired
	private TaskService taskService;


	@ApiOperation(value = "批准这个请假流程")
	@PostMapping("/designateByAskForLeaveId")
	@Transactional
	public SuccessVo designateByAskForLeaveId(@RequestBody Map map) {

		AskForLeave askForLeave = new AskForLeave();
		askForLeave.setUpdateTime(new Date());
		askForLeave.setId(Integer.valueOf(String.valueOf(map.get("askForLeaveId"))));
		this.askForLeaveMapper.updateByPrimaryKeySelective(askForLeave);
		this.taskService.setAssignee(String.valueOf(map.get("taskId")), String.valueOf(map.get("designatePersonid")));

		return new SuccessVo("处理成功");
	}

//

	@ApiOperation(value = "查询请假历史(通过登录人id)")
	@GetMapping("/selectFlowDisposeByLoginId/{loginId}")
	@Transactional
	public SuccessVo selectFlowDisposeByLoginId(@PathVariable String loginId) {
		List<FlowDisposeBO> flowDisposeBOList = this.askForLeaveMapper.selectFlowDisposeByLoginId(loginId);

		return new SuccessVo(flowDisposeBOList);
	}


	@ApiOperation(value = "批准这个请假流程")
	@PostMapping("/ratify")
	@Transactional
	public SuccessVo ratify(@RequestBody Map map) {


		AskForLeave askForLeave = new AskForLeave();
		askForLeave.setId(Integer.valueOf(String.valueOf(map.get("askForLeaveId"))));
		askForLeave.setUpdateTime(new Date());

		int i = this.askForLeaveMapper.updateByPrimaryKeySelective(askForLeave);
		if (i == 1) {
			this.taskService.complete(String.valueOf(map.get("taskId")));
		}


		return new SuccessVo("处理成功");
	}


	/**
	 * 查询请假实例信息
	 *
	 * @return
	 */
	@ApiOperation(value = "查询历史请假流程")
	@PostMapping("/selecthistoricalProcess")
	@Transactional
	public SuccessVo selecthistoricalProcess(@RequestBody Map map) {
		List<Map> list = this.askForLeaveMapper.selecthistoricalProcess(map);
		return new SuccessVo(list);
	}


	/**
	 * 查询请假实例信息
	 *
	 * @return
	 */
	@ApiOperation(value = "查询请假实例信息")
	@PostMapping("/selectLeaveInstance")
	@Transactional
	@RequiresPermissions("nodeExamine")
	public SuccessVo selectLeaveInstance(@RequestBody NodeExamineBo nodeExamineVo) {
		Integer loginId = nodeExamineVo.getLoginId();
		Map<String, Object> map = new HashMap<>();
		map.put("loginId", loginId);
		//3.根据流程定义的key,负责人assignee来实现当前用户的任务列表查询
		List<ActRuTaskBO> actRuTaskVOS = this.askForLeaveMapper.selectLeaveExamineByAssignee(map);
		return new SuccessVo(actRuTaskVOS);
	}


	@ApiOperation(value = "插入", notes = "")
	@PostMapping("/insert")
	@Transactional
	public SuccessVo insert(@RequestBody ProcessDefinitionBO processDefinitionVo) {


		Map<String, Object> variables = new HashMap<>();
		variables.put("staff", "46");//设置
		variables.put("hr", "47");//设置hr是谁

		String uuid32 = UuidUtil.getUUID32();

		identityService.setAuthenticatedUserId(processDefinitionVo.getLoginId());

		//开启请假流程
		ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("qingjia01", uuid32, variables);

		//保存请假信息到请假数据表里面.
		AskForLeave askForLeave = new AskForLeave();
		askForLeave.setCause(processDefinitionVo.getCause());
		askForLeave.setLoginId(Integer.valueOf(processDefinitionVo.getLoginId()));
		askForLeave.setBusinessKey(uuid32);

		//设置登录人id 也就是操作人的主键
		askForLeave.setLoginId(Integer.valueOf(processDefinitionVo.getLoginId()));
		askForLeave.setProcessInstanceId(processInstance.getProcessInstanceId());

		int insert = this.askForLeaveMapper.insertSelective(askForLeave);

		if (insert == 0) {
			throw new ExceptionUtil("启动请假流程失败");
		}

		return new SuccessVo(processDefinitionVo);
	}
}
